<template>
<!-- 管控措施管理 -->
  <div class="countrylogo">
    <basic-container>
      <avue-crud 
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >

        <!-- 左上方菜单内容 -->
        <template slot="menuLeft">
          <el-button 
            type="danger"
            size="small"
            icon="el-icon-delete"
            plain
            v-if="permission.controlmeasures_delete"
            @click="handleDelete">
            删 除
          </el-button>
        </template>

        <!-- 图片显示自定义 -->
        <template slot="bzxs" slot-scope="scope">
          <div>
            <el-image 
              style="width: 80px; height: 80px"
              :src="scope.row['fileUrl']"
              :preview-src-list="[scope.row['fileUrl']]">
            </el-image>
          </div>
        </template>

        <!-- 图片表单自定义 -->
        <template slot="bladeFileIdForm">
          <div>
            <el-upload
              accept="image/*" 
              action="/api/fileManager/upload/file"
              :headers="SetHeaders"
              :file-list="tpFileList"
              :before-upload="beforeUploadTP" 
              :on-success="uploadTPSuccess">
              <el-button size="small" type="primary">上传图片</el-button>
            </el-upload>
          </div>
        </template>

        <!-- 图片内容表单自定义 -->
        <template slot="tpContentForm">
          <div class="tp_content">
            <el-tag
              v-for="(item, index) in tpFileList"
              :key="index"
              type="info" 
              closable
              @close="handleRemoveTP(index)"
            >
              <el-image 
                style="width: 100px; height: 100px"
                :src="item.response.data.fileUrl">
              </el-image>
            </el-tag>
          </div>
        </template>

      </avue-crud>

    </basic-container>
  </div>
</template>

<script>
import { getList, remove, add, update } from "@/api/configdb/countrylogo";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      selectionList: [],
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 4,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "标志编号",
            prop: "id",
            align: "center",
            addDisplay: false,
            viewDisplay: false,
            editDisplay: false
          },
          {
            label: "标志名称",
            prop: "sjmc",
            align: "center",
            search: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入标志名称",
              trigger: "blur"
            }]
          },
          {
            label: "标志显示",
            prop: "bzxs",
            align: "center",
            slot: true,
            addDisplay: false,
            viewDisplay: false,
            editDisplay: false
          },
          {
            label: "图片类型",
            prop: "wjlx",
            align: "center",
            span: 24,
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=country_logo_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请选择图片类型",
              trigger: "blur"
            }]
          },
          {
            label: "图片",
            prop: "bladeFileId",
            hide: true,
            span: 24,
            formslot: true,
            viewDisplay: false,
            rules: [{
              required: true,
              message: "请选择图片",
              trigger: "blur"
            }]
          },
          {
            label: "",
            prop: "tpContent",
            hide: true,
            span: 24,
            formslot: true,
          }
        ]
      },
      // 附件
      tpFileList: [],
      SetHeaders: { "Blade-Auth": getToken() },
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 表格按钮权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.countrylogo_add, false),
        viewBtn: this.vaildData(this.permission.countrylogo_view, false),
        delBtn: this.vaildData(this.permission.countrylogo_delete, false),
        editBtn: this.vaildData(this.permission.countrylogo_edit, false)
      };
    },

    // 勾选项id
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化数据
    initData() {
      this.onLoad(this.page);
    },

    // 新增
    rowSave(row, done) {
      for(let key in row) {
        if(key.indexOf("$") >= 0) {
          delete row[key];
        }
      }
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      });
    },

    // 编辑
    rowUpdate(row, index, done) {
      for(let key in row) {
        if(key.indexOf("$") >= 0) {
          delete row[key];
        }
      }
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      });
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(row.id);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
      });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(this.ids);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        this.$refs.crud.toggleSelection();
      });
    },

    // 新增、查看、编辑弹窗打开前
    beforeOpen(done, type) {
      this.tpFileList = [];
      if(type === "edit" && this.form.bladeFileId !== "" && JSON.stringify(this.form.bladeFile) !== "{}") {
        this.tpFileList.push({
          name: this.form.bladeFile.fileName,
          response: {
            code: 200,
            msg: "操作成功",
            success: true,
            data: this.form.bladeFile
          }
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 换页数
    currentChange(currentPage){
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格内容
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = [];
        data.records.forEach(item => {
          this.data.push({
            id: item.id,
            sjmc: item.sjmc,
            wjlx: item.wjlx,
            bladeFileId: item.bladeFileId,
            bladeFile: item.bladeFile,
            fileUrl: item.bladeFile.fileUrl
          });
        });
        this.loading = false;
        this.selectionClear();
      });
    },

    /**
     * 上传附件
    */

    // 上传图片前
    beforeUploadTP(file) {
      var isLimit = (this.tpFileList.length + 1) <= 1 ;
      if (!isLimit) {
        this.$message.warning('只能选择一个图片!');
      }
      return isLimit;
    },

    // 上传图片成功
    uploadTPSuccess(response, file, fileList) {
      if(response.code !== 200 || response.success !== true) {
        return;
      }
      this.tpFileList = fileList;
      this.form.bladeFileId = response.data.id;
    },

    // 删除图片
    handleRemoveTP(index) {
      this.tpFileList.splice(index, 1);
      this.form.bladeFileId = "";
    }
    
  }
};
</script>

<style lang="scss" scpoed>
.countrylogo {
  .avue-crud {
    .avue-crud__search {
      .el-form-item__label {
        text-align: unset;
      }
    }
  }
}

.view_picture_footer {
  text-align: center;
  margin-top: 15px;

  .el-button {
      width: 100px;
      height: 35px;
  }
}

.el-carousel {
  .el-carousel__container {
    height: 500px;
    text-align: center;

    img {
      height: 100%;
    }
  }
}

.el-upload-list {
  display: none;
}

.tp_content {
  .el-tag {
    height: 100px;
    line-height: 100px;
    padding: 0px 10px 0px 0px;
    margin-right: 10px;
    margin-top: 10px;
    
    .el-icon-close {
      top: -84px;
    }
  }

  img {
    width: 98px;
    height: 98px;
    cursor: pointer;
  }
}

.avue-dialog {
  .el-dialog__body {
    .avue-form__row:nth-child(3) {
      .el-form-item {
        margin-bottom: 0px;
      }
    }
  }
}
</style>